<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="小组名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupName">
              <a-input
                :disabled="formDisabled"
                v-model="model.groupName"
                :max-length="30"
                placeholder="请输入小组名称"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.groupType == 'public'">
            <a-form-model-item label="小组密级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupDense">
              <j-dict-select-tag
                type="list"
                :disabled="formDisabled"
                v-model="model.groupDense"
                dictCode="group_intensive_type"
                placeholder="请选择小组密级"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="小组成员子表" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupMembers">
              <select-user-by-dep :multi="true" v-model="model.groupMembers" placeholder="请选择付款人" :disabled="formDisabled" @back="backGroup"/>
              <a-table
                ref="table"
                size="middle"
                :scroll="{ x: true }"
                bordered
                rowKey="empId"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="false"
                class="j-table-force-nowrap"
              >
                <span slot="action" slot-scope="text, record">
                  <template v-if="!disabled">
                    <a-popconfirm title="确定删除吗?" @confirm="() => deleteGroup(record)">
                      <a-button
                        class="ruleButton"
                        :ghost="true"
                        type="primary"
                        icon="delete"
                      />
                    </a-popconfirm>
                  </template>
                </span>
              </a-table>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled="formDisabled" :max-length="200" v-model="model.remark" rows="4" placeholder="请输入备注"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import { mapGetters } from 'vuex'

export default {
  name: 'ErpActQuitConfigForm',
  components: {
    SelectUserByDep
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        groupName: [
          { required: true, message: '请输入小组名称!' }
        ],
        groupDense: [
          { required: true, message: '请选择小组密级!' }
        ],
        groupMembers: [
          { required: true, message: '请选择小组成员子表!' }
        ]
      },
      url: {
        add: '/api/erp/publish/erpPublishContactGroup/add',
        edit: '/api/erp/publish/erpPublishContactGroup/edit'
      },
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '用户姓名',
          align: 'center',
          dataIndex: 'realname'
        },
        {
          title: '部门',
          align: 'center',
          dataIndex: 'orgCodeTxt'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 150,
          scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action'
          }
        }
      ],
      dataSource: [],
      dictOptions: {}
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    ...mapGetters(['userInfo']),
    deleteGroup(record) {
      this.dataSource.splice(this.dataSource.findIndex(v => v.empId == record.empId), 1)
      this.$set(this.model, 'groupMembers', this.dataSource.map(v => v.empId).join(','))
    },
    backGroup(list) {
      this.dataSource = list
    },
    add(record) {
      this.modelDefault = {
        ...record
      }
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.$refs.form.clearValidate()
      this.model = Object.assign({}, record)
      this.visible = true
      this.dataSource = []
      if (record.id) {
        getAction('/api/erpmng/sys/user/queryByEmpIds', { empId: record.groupMembers }).then((res) => {
          if (res.success) {
            this.dataSource = res.result || []
          }
        });
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
                that.$emit('unButtonDisable')
              }
            })
            .catch((error) => {
              that.$emit('unButtonDisable')
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          that.$emit('unButtonDisable')
        }
      })
    }
  }
}
</script>
